package com.lzy.ninegridview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.ninegridview.callback.NewsCallBack;
import com.lzy.ninegridview.model.news.NewsContentAdapter;
import com.lzy.ninegridview.model.news.bean.NewsContent;
import com.lzy.ninegridview.model.news.bean.NewsImage;
import com.lzy.ninegridview.utils.Urls;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import okhttp3.Request;
import okhttp3.Response;

/**
 * ================================================
 * 作    者：廖子尧
 * 版    本：1.0
 * 创建日期：2016/3/20
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ImageGridFragment extends Fragment {

    //    @Bind(R.id.ptr)
    PtrClassicFrameLayout ptr;
    //    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<NewsContent> newsContentList;
    private NewsContentAdapter mAdapter;
    private String channelId;
    private int page;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        ButterKnife.bind(this, view);
        recyclerView = view.findViewById(R.id.recyclerView);
        ptr = view.findViewById(R.id.ptr);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new NewsContentAdapter(getActivity(), new ArrayList<>());
        recyclerView.setAdapter(mAdapter);

        //initData(false);

        ptr.setLastUpdateTimeRelateObject(this);
        ptr.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                initData(true);
            }
        });

        return view;
    }

    private void initData(final boolean isMore) {
        HttpParams params = new HttpParams();
        params.put("channelId", channelId);
        params.put("page", String.valueOf(page));
        OkHttpUtils.get(Urls.NEWS)//
                .tag(this)//
                .params(params)//
                .cacheKey("NEWS")//
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)//
                .execute(new NewsCallBack() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONArray object = new JSONObject(s).getJSONObject("showapi_res_body").getJSONObject("pagebean").getJSONArray("contentlist");
                            Type newsContentType = new TypeToken<List<NewsContent>>() {
                            }.getType();
                            if (isMore) {
                                List<NewsContent> more = new Gson().fromJson(object.toString(), newsContentType);
                                newsContentList.addAll(0, more);
                            } else {
                                newsContentList = new Gson().fromJson(object.toString(), newsContentType);
                            }
                            mAdapter.setData(newsContentList);
                            page++;
                            ptr.refreshComplete();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayList<NewsContent> data = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            int finalI = i;
            data.add(new NewsContent() {
                {
                    setTitle("title" + finalI);
                    setDesc("desc" + finalI);
                    setChannelId("id" + finalI);
                    setPubDate("20241202-" + finalI);
                    setSource("sou" + finalI);
                    ArrayList<NewsImage> imageurls = new ArrayList<>();
                    for (int i = 0; i < 9; i++) {
                        NewsImage image = new NewsImage();
                        image.setUrl("https://oss.res.gomezsz.com/oss-res/time-res/uat/8f20f84104d14a2999c03abb9a3f7792.jpg");
                        imageurls.add(image);
                    }
                    setImageurls(imageurls);
                }
            });
        }
        mAdapter.setData(data);
    }
}
